package com.cloudkinto.service.stock.impl;

import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.dao.ProductStockRackDao;
import com.cloudkinto.entity.ProductStockFlowingDo;
import com.cloudkinto.dao.ProductStockFlowingDao;
import com.cloudkinto.extentity.ProductStockDetailDto;
import com.cloudkinto.service.stock.ProductStockFlowingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  service实现类
 * </p>
 * @author hua
 * @since 2023-01-04
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class ProductStockFlowingServiceImpl extends ServiceImpl<ProductStockFlowingDao, ProductStockFlowingDo> implements ProductStockFlowingService {
    @Autowired
    private ProductStockFlowingDao dao;
    @Autowired
    private ProductStockRackDao productStockRackDao;

    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public Object add(Object req, Long userId) {
        ProductStockFlowingDo entityDo = new ProductStockFlowingDo();
        BeanUtils.copyProperties(req, entityDo);
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        return handlerAddAfter(entityDo);
    }

    //新增校验
    private void addCheck(Object req) {

    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(ProductStockFlowingDo entityDo, Long userId) {
        entityDo.setCreateBy(userId);
        entityDo.setCreateTime(new Date());
        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }

    //将新增的do转换成要返回的实体
    private Object handlerAddAfter(ProductStockFlowingDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success("");
    }

    @Override
    public Object update(Object req, Long userId) {
        /*ProductStockFlowingDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);*/
        return null;
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(ProductStockFlowingDo entityDo, Long userId) {
        entityDo.setUpdateBy(userId);
        entityDo.setUpdateTime(new Date());
    }

    //做修改之后要执行的业务定制处理
    private Object handlerUpdateAfter(ProductStockFlowingDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        ProductStockFlowingDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        return dao.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(ProductStockFlowingDo entityDo, Long userId) {
    }

    @Override
    public Object detail(Long id) {
        ProductStockFlowingDo entityDo =  dao.selectById(id);
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private Object handlerDetailAfter(Object res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public List<Object> getList(Map<String, String> map) {
        QueryWrapper<ProductStockFlowingDo> wrapper = queryBuild(map);
        List<ProductStockFlowingDo> list = this.list(wrapper);
        List<Object> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<ProductStockFlowingDo> wrapper = queryBuild(map);
        IPage<ProductStockFlowingDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<Object> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int)page.getCurrent(), (int)page.getSize(), (int)page.getTotal(), (int)page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<Object> handlerListAfter(List<ProductStockFlowingDo> list ){
        List<Object> result = new ArrayList<>();
        return result;
    }

    private IPage<ProductStockFlowingDo> pageInit(Map<String, String> map) {
        IPage<ProductStockFlowingDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<ProductStockFlowingDo> queryBuild(Map<String, String> map) {
        QueryWrapper<ProductStockFlowingDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("keyword", map.get("keyword"));
        }
        return wrapper;
    }
    @Override
    public void insertFlowing(String type, Long productId, Long storageId, int goodQuantity, int badQuantity, Long toRackId, Long companyId, Long userId, String tips) {
        this.insertFlowing(type, null, productId, storageId, goodQuantity, badQuantity, 0, toRackId, companyId, userId, tips);
    }

    @Override
    public Long insertFlowing(String type, Long shopId, Long productId, Long storageId, int goodQuantity, int badQuantity, Long toRackId, Long companyId, Long userId, String tips) {
        return this.insertFlowing(type, shopId, productId, storageId, goodQuantity, badQuantity, 0, toRackId, companyId, userId, tips);
    }

    public Long insertFlowing(String type, Long shopId, Long productId, Long storageId, int goodQuantity, int badQuantity, int goodLockingQuantity, Long toRackId, Long companyId, Long userId, String tips) {
        return this.insertFlowing(type, "", shopId, productId, storageId, goodQuantity, badQuantity, goodLockingQuantity, toRackId, null, companyId, userId, tips);
    }

    public Long insertFlowing(String type, String orderNo, Long shopId, Long productId, Long storageId, int goodQuantity, int badQuantity, int goodLockingQuantity, Long toRackId, Long companyId, Long userId, String tips) {
        return this.insertFlowing(type, orderNo, shopId, productId, storageId, goodQuantity, badQuantity, goodLockingQuantity, toRackId, null, companyId, userId, tips);
    }

    @Override
    public Long insertFlowing(String type, String orderNo, Long shopId, Long productId, Long storageId, int goodQuantity, int badQuantity, int goodLockingQuantity, Long toRackId, String status, Long companyId, Long userId, String tips) {
        //库存流水新
        ProductStockFlowingDo flowingDo = new ProductStockFlowingDo();
        flowingDo.setType(type);
        flowingDo.setNoteNo(orderNo);
        flowingDo.setProductId(productId);
        flowingDo.setStorageId(storageId);
        flowingDo.setGoodQuantity(goodQuantity);
        flowingDo.setBadQuantity(badQuantity);
        flowingDo.setGoodLockingQuantity(goodLockingQuantity);
        flowingDo.setRackId(toRackId);
        flowingDo.setCreateTime(new Date());
        flowingDo.setCreateBy(userId);
        flowingDo.setCompanyId(companyId);
        flowingDo.setTips(tips);
        flowingDo.setShopId(shopId);
        flowingDo.setStatus(status);
        flowingDo.setFinishTime(new Date());
        flowingDo.setFinishBy(userId);

        //查询该库位剩余库存
        Map<String, String> map = new HashMap<>();
        map.put("productId", productId + "");
        if (toRackId != null) {
            map.put("rackId", toRackId + "");
        }
        map.put("storageId", storageId + "");
        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        map.put("notStore", "1");
        ProductStockDetailDto detailDto = productStockRackDao.getStockByRackId(map);
        if (detailDto != null) {
            flowingDo.setLeftGoodQuantity(detailDto.getGoodQuantity());
            flowingDo.setLeftBadQuantity(detailDto.getBadQuantity());
            flowingDo.setLeftGoodLockingQuantity(detailDto.getGoodLockQuantity());
            flowingDo.setLeftBadLockingQuantity(detailDto.getBadLockQuantity());
        }
        else {
            flowingDo.setLeftGoodQuantity(0);
            flowingDo.setLeftBadQuantity(0);
            flowingDo.setLeftGoodLockingQuantity(0);
            flowingDo.setLeftBadLockingQuantity(0);
        }

        dao.insert(flowingDo);
        return flowingDo.getId();
    }

}
